<!DOCTYPE html>

<html>
<head>
  <title>controller.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page_wrapper">
            <div id="jump_page">
              
                
                <a class="source" href="app.html">
                  app.js
                </a>
              
                
                <a class="source" href="controller.html">
                  controller.js
                </a>
              
                
                <a class="source" href="helpers.html">
                  helpers.js
                </a>
              
                
                <a class="source" href="model.html">
                  model.js
                </a>
              
                
                <a class="source" href="view.html">
                  view.js
                </a>
              
            </div>
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>controller.js</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-comment">/* global $, window */</span></pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>This module contains and exposes the
Data Controller. This is where all the SAFE-Access happens</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">MODULE</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>helper function to generate a random string for us</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">generateRandomString</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> text = <span class="hljs-string">''</span>
    <span class="hljs-keyword">let</span> possible = <span class="hljs-string">'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
      text += possible.charAt(<span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * possible.length))
    }
    <span class="hljs-keyword">return</span> text
  }</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>The Controller holds the data and is the bridge to interact with
SAFE.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Controller</span> </span>{
    <span class="hljs-keyword">constructor</span> () {</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>setup the internal state</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">this</span>._hostName = <span class="hljs-built_in">window</span>.location.host.replace(<span class="hljs-regexp">/.safenet$/g</span>, <span class="hljs-string">''</span>)
      <span class="hljs-keyword">this</span>._LOCAL_STORAGE_TOKEN_KEY = <span class="hljs-string">`SAFE_TOKEN_<span class="hljs-subst">${<span class="hljs-keyword">this</span>._hostName}</span>`</span>
      <span class="hljs-keyword">this</span>._authToken = <span class="hljs-literal">null</span>
      <span class="hljs-keyword">this</span>._currentPostHandleId = <span class="hljs-literal">null</span>
      <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle = <span class="hljs-literal">null</span>
      <span class="hljs-keyword">this</span>._symmetricCipherOptsHandle = <span class="hljs-literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>ensure we are cleaning up properly before closing</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      $(<span class="hljs-built_in">window</span>).on(<span class="hljs-string">'beforeunload'</span>, () =&gt; {
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._currentPostHandleId) {
          <span class="hljs-built_in">window</span>.safeAppendableData.dropHandle(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId)
        }
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._symmetricCipherOptsHandle) {
          <span class="hljs-built_in">window</span>.safeCipherOpts.dropHandle(<span class="hljs-keyword">this</span>._symmetricCipherOptsHandle)
        }
      })</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>start up the data container</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">this</span>._data = <span class="hljs-keyword">new</span> MODULE.DataContainer()
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Whenever something changes, the controller will emit an
event. The view (and any other interested party) can listen
to events and react accordingly.</p>
<p>At the moment, the controller emits the follwing events:</p>
<ul>
<li><code>comments-updated</code> : the comments have changed</li>
<li><code>user-updated</code>     : the user object has changed</li>
</ul>

            </div>
            
            <div class="content"><div class='highlight'><pre>    on (name, fn) {
      <span class="hljs-keyword">return</span> $(<span class="hljs-keyword">this</span>).on(name, fn)
    }

    emit (name, param) {
      <span class="hljs-keyword">return</span> $(<span class="hljs-keyword">this</span>).trigger(name, param)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Once all is linked up, this is called to start up the connection
to save</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    init () {
      <span class="hljs-keyword">this</span>._authToken = <span class="hljs-built_in">window</span>.safeAuth.getAuthToken(<span class="hljs-keyword">this</span>._LOCAL_STORAGE_TOKEN_KEY)</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>check if we have a local auth token</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> (<span class="hljs-keyword">this</span>._authToken</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>if so, skip ahead to load DNS</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          ? <span class="hljs-keyword">this</span>._getDns()</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>if not we need to start by authorising the App</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          : <span class="hljs-keyword">this</span>._authoriseApp())</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>either way, once we have this setup, go ahead and fetch the comments</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>.fetchComments())
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>helper to gain access to the internal data</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getData () {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._data
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>is the current user owner of this URL</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    isAdmin () {
      <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._isDevMode() &amp;&amp; <span class="hljs-keyword">this</span>._data.user.dns) {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
      }

      <span class="hljs-keyword">let</span> currentDns = <span class="hljs-keyword">this</span>._hostName.replace(<span class="hljs-regexp">/(^\w+\.|.safenet$)/g</span>, <span class="hljs-string">''</span>)
      <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>._data.user.dns) {
        <span class="hljs-keyword">return</span>
      }
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._data.user.dns.indexOf(currentDns) !== <span class="hljs-number">-1</span>
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>are comments generally enabled yet?</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    commentsEnabled () {
      <span class="hljs-keyword">return</span> !!<span class="hljs-keyword">this</span>._currentPostHandleId
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Global activities</p>

            </div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>If comments aren’t enabled yet, this call allows one to start them
by creating the initial appendable data entry.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    enableComments () {</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>create appendable dataHandleId for location</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeAppendableData.create(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._getLocation(), <span class="hljs-literal">false</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>remap handleID</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.__parsedResponseBody__.handleId)
        .then(<span class="hljs-function">(<span class="hljs-params">handleId</span>) =&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>now put the handleID, this actually creates the appendableData</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeAppendableData.put(<span class="hljs-keyword">this</span>._authToken, handleId)</pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>after: store the handleID for later reuse</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> { <span class="hljs-keyword">this</span>._currentPostHandleId = handleId }))
        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>then emit to inform listeners that the comments state has changed</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'comments-updated'</span>)
        })
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>comment activities</p>
<p>Fetch all comments from the network</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    fetchComments () {</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>start by clearing the list</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">this</span>._data.commentList = []

      <span class="hljs-keyword">const</span> fetchAll = <span class="hljs-function">(<span class="hljs-params">totalComments</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> all = []
        
        <span class="hljs-keyword">if</span> (totalComments === <span class="hljs-number">0</span>) {
          <span class="hljs-keyword">return</span>;
        } 
        
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; totalComments; i++) {
          all.push(i)
        }</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>fetch all the items in parallel, one at each index</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.all(
          all.map(<span class="hljs-function"><span class="hljs-params">index</span> =&gt;</span> <span class="hljs-keyword">this</span>._fetchComment(index).then(<span class="hljs-function">(<span class="hljs-params">c</span>) =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p>for each item found, append them to the commentsList</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            <span class="hljs-keyword">this</span>._data.commentList.push({
              <span class="hljs-attr">index</span>: index,
              <span class="hljs-attr">comment</span>: c
            })
          })))
      }</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>learn how many items there are in our list</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">const</span> getCommentsListLength = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        MODULE.log(<span class="hljs-string">'Fetch appendable data length'</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeAppendableData.getMetadata(
              <span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId)
          .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.__parsedResponseBody__.dataLength)
      }</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <p>fetch the actual appendableData</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">const</span> fetchCommentsListing = <span class="hljs-function">(<span class="hljs-params">dataHandleId</span>) =&gt;</span> {
        MODULE.log(<span class="hljs-string">'Fetch appendable data'</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeAppendableData.getHandle(
              <span class="hljs-keyword">this</span>._authToken, dataHandleId)
          .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> { <span class="hljs-keyword">this</span>._currentPostHandleId = res.__parsedResponseBody__.handleId })
      }</pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>tying it all together</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">const</span> fetchComments = <span class="hljs-function">(<span class="hljs-params">handleId</span>) =&gt;</span>
        <span class="hljs-built_in">Promise</span>.resolve(handleId)</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <p>first fetch the listing itself</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          .then(fetchCommentsListing)</pre></div></div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-32">&#182;</a>
              </div>
              <p>learn about the count</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          .then(getCommentsListLength)</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-33">&#182;</a>
              </div>
              <p>then fetch each item</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          .then(fetchAll)</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-34">&#182;</a>
              </div>
              <p>once we have all comments, sort them</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>._sortComments())</pre></div></div>
            
        </li>
        
        
        <li id="section-35">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-35">&#182;</a>
              </div>
              <p>and emit the events, even if something failed</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          .then(<span class="hljs-function">(<span class="hljs-params">r</span>) =&gt;</span> <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'comments-updated'</span>),
                (e) =&gt; {
                  MODULE.log(e)
                  <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'comments-updated'</span>)
                  <span class="hljs-keyword">return</span> e
                })</pre></div></div>
            
        </li>
        
        
        <li id="section-36">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-36">&#182;</a>
              </div>
              <p>put it all in motion:</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-37">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-37">&#182;</a>
              </div>
              <p>get handle for appendable data</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-built_in">window</span>.safeDataId.getAppendableDataHandle(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._getLocation()),</pre></div></div>
            
        </li>
        
        
        <li id="section-38">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-38">&#182;</a>
              </div>
              <p>fetch the comments with that handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        fetchComments,</pre></div></div>
            
        </li>
        
        
        <li id="section-39">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-39">&#182;</a>
              </div>
              <p>release teh appendable data handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        (dataIdHandle) =&gt; <span class="hljs-built_in">window</span>.safeDataId.dropHandle(<span class="hljs-keyword">this</span>._authToken, dataIdHandle))
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-40">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-40">&#182;</a>
              </div>
              <p>Posting a new comment</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    postComment (comment, publicName) {
      MODULE.log(<span class="hljs-string">`Writing comment @<span class="hljs-subst">${publicName}</span>: <span class="hljs-subst">${comment}</span>`</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-41">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-41">&#182;</a>
              </div>
              <p>convert the input into data SAFEnet understands</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">const</span> timeStamp = (<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()).getTime()
      <span class="hljs-keyword">const</span> name = publicName + timeStamp + generateRandomString()
      <span class="hljs-keyword">const</span> payload = <span class="hljs-keyword">new</span> Buffer(<span class="hljs-built_in">JSON</span>.stringify({
        <span class="hljs-attr">name</span>: publicName,
        <span class="hljs-attr">comment</span>: comment,
        <span class="hljs-attr">time</span>: timeStamp
      })).toString(<span class="hljs-string">'base64'</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-42">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-42">&#182;</a>
              </div>
              <p>and off it goes</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-43">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-43">&#182;</a>
              </div>
              <p>get handle for to be created comment</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeStructuredData.create(<span class="hljs-keyword">this</span>._authToken, name, <span class="hljs-number">500</span>, payload),</pre></div></div>
            
        </li>
        
        
        <li id="section-44">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-44">&#182;</a>
              </div>
              <p>with that handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (currentSDHandleId) =&gt; <span class="hljs-built_in">window</span>.safeStructuredData.put(<span class="hljs-keyword">this</span>._authToken, currentSDHandleId)</pre></div></div>
            
        </li>
        
        
        <li id="section-45">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-45">&#182;</a>
              </div>
              <p>save the data then</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-46">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-46">&#182;</a>
              </div>
              <p>replace the structured Data handle for a dataID handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>              <span class="hljs-built_in">window</span>.safeStructuredData.getDataIdHandle(<span class="hljs-keyword">this</span>._authToken, currentSDHandleId),</pre></div></div>
            
        </li>
        
        
        <li id="section-47">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-47">&#182;</a>
              </div>
              <p>append that handle to the appendable data</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>              (dataIdHandle) =&gt; <span class="hljs-built_in">window</span>.safeAppendableData.append(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, dataIdHandle),</pre></div></div>
            
        </li>
        
        
        <li id="section-48">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-48">&#182;</a>
              </div>
              <p>release the dataId handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>              (dataIdHandle) =&gt; <span class="hljs-built_in">window</span>.safeDataId.dropHandle(<span class="hljs-keyword">this</span>._authToken, dataIdHandle)
            )),</pre></div></div>
            
        </li>
        
        
        <li id="section-49">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-49">&#182;</a>
              </div>
              <p>release the structured data handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (currentSDHandleId) =&gt; <span class="hljs-built_in">window</span>.safeStructuredData.dropHandle(<span class="hljs-keyword">this</span>._authToken, currentSDHandleId))</pre></div></div>
            
        </li>
        
        
        <li id="section-50">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-50">&#182;</a>
              </div>
              <p>once done, refresh the comments listing</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>.fetchComments())
        .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> {
          <span class="hljs-built_in">window</span>.alert(<span class="hljs-string">''</span>);
        });
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-51">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-51">&#182;</a>
              </div>
              <p>Delete a comment at the given index</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>
    deleteComment (index) {</pre></div></div>
            
        </li>
        
        
        <li id="section-52">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-52">&#182;</a>
              </div>
              <p>prepare the removable of a specific index</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeAppendableData.removeAt(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, index)
        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-53">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-53">&#182;</a>
              </div>
              <p>send that off</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeAppendableData.post(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId))
        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-54">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-54">&#182;</a>
              </div>
              <p>clear our local cache</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeAppendableData.clearAll(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, <span class="hljs-literal">true</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-55">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-55">&#182;</a>
              </div>
              <p>and refresh all comments</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>.fetchComments())
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-56">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-56">&#182;</a>
              </div>
              <p>user blocking management</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>
    blockUser (userName, index) {</pre></div></div>
            
        </li>
        
        
        <li id="section-57">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-57">&#182;</a>
              </div>
              <p>get appendable data signed key at index</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(
          <span class="hljs-built_in">window</span>.safeAppendableData.getSignKeyAt(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, index),
          (signKeyHandleId) =&gt;
            <span class="hljs-built_in">window</span>.safeAppendableData.addToFilter(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, [signKeyHandleId])
              .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">window</span>.safeAppendableData.post(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId))
              .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>._saveBlockedUser(userName, signKeyHandleId))
              .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>.fetchComments()),
          (signKeyHandleId) =&gt; <span class="hljs-built_in">window</span>.safeSignKey.dropHandle(<span class="hljs-keyword">this</span>._authToken, signKeyHandleId))
        .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'comments-updated'</span>))
    }

    unblockUser (userName) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-58">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-58">&#182;</a>
              </div>
              <p>get a serialiased key</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-built_in">window</span>.safeSignKey.deserialise(<span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">new</span> Buffer(<span class="hljs-keyword">this</span>._data.blockedUsers[userName], <span class="hljs-string">'base64'</span>)),
        (signKeyHandle) =&gt;
          <span class="hljs-built_in">window</span>.safeAppendableData.removeFromFilter(
            <span class="hljs-keyword">this</span>._authToken,
            <span class="hljs-keyword">this</span>._currentPostHandleId,
            [signKeyHandle])
          .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> <span class="hljs-built_in">window</span>.safeAppendableData.post(
              <span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId)
          .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> {
            <span class="hljs-keyword">delete</span> <span class="hljs-keyword">this</span>._data.blockedUsers[userName]
            <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> Buffer(<span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-keyword">this</span>._data.blockedUsers)).toString(<span class="hljs-string">'base64'</span>)
            <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeStructuredData.updateData(
                <span class="hljs-keyword">this</span>._authToken,
                <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle,
                data, <span class="hljs-keyword">this</span>._symmetricCipherOptsHandle)
              .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> <span class="hljs-built_in">window</span>.safeStructuredData.post(
                    <span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle)
              )
          }
          )
        ),</pre></div></div>
            
        </li>
        
        
        <li id="section-59">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-59">&#182;</a>
              </div>
              <p>release signing key</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        (signKeyHandle) =&gt; <span class="hljs-built_in">window</span>.safeSignKey.dropHandle(<span class="hljs-keyword">this</span>._authToken, signKeyHandle)
      )
    }

    hasAuthToken() {
      <span class="hljs-keyword">return</span> !!<span class="hljs-keyword">this</span>._authToken;
    }

    hasBlockedUsers () {
      <span class="hljs-keyword">return</span> (<span class="hljs-keyword">this</span>._data.blockedUsers &amp;&amp; (<span class="hljs-built_in">Object</span>.keys(<span class="hljs-keyword">this</span>._data.blockedUsers).length !== <span class="hljs-number">0</span>));
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-60">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-60">&#182;</a>
              </div>
              <p>Internals</p>

            </div>
            
        </li>
        
        
        <li id="section-61">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-61">&#182;</a>
              </div>
              <p>figure out the location the information is to be stored</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _getLocation () {
      <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._isDevMode() &amp;&amp; <span class="hljs-keyword">this</span>._data.user.dns) {
        <span class="hljs-keyword">return</span> <span class="hljs-string">`comments-dev-<span class="hljs-subst">${<span class="hljs-keyword">this</span>._data.user.dns}</span>/<span class="hljs-subst">${<span class="hljs-built_in">window</span>.location.pathname}</span>`</span>
      }
      <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-keyword">this</span>._hostName}</span>/<span class="hljs-subst">${<span class="hljs-built_in">window</span>.location.pathname}</span>`</span>
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-62">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-62">&#182;</a>
              </div>
              <p>if we are running from localhost, put the app into developer mode</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _isDevMode () {
      <span class="hljs-keyword">return</span> !!<span class="hljs-keyword">this</span>._hostName.match(<span class="hljs-regexp">/^localhost(:[\d]+)?$/</span>)
    }

    _getCypher () {
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeCipherOpts.getHandle(
          <span class="hljs-keyword">this</span>._authToken,
          <span class="hljs-built_in">window</span>.safeCipherOpts.getEncryptionTypes().SYMMETRIC)
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> { <span class="hljs-keyword">this</span>._symmetricCipherOptsHandle = res.__parsedResponseBody__.handleId })
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-63">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-63">&#182;</a>
              </div>
              <p>Helper function for a typical use case:</p>
<ol>
<li>get a data handle (as <code>promise</code>)</li>
<li>run the code <code>fn</code> (with the handle as the first parameter)</li>
<li>once execution completed, clean up the handle by calling <code>release</code></li>
</ol>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _autoRelease (promise, fn, release) {
      <span class="hljs-keyword">return</span> promise
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.__parsedResponseBody__ ? res.__parsedResponseBody__.handleId : res)
        .then(<span class="hljs-function"><span class="hljs-params">handleId</span> =&gt;</span> fn(handleId)
          .then(<span class="hljs-function">(<span class="hljs-params">r</span>) =&gt;</span> release(handleId).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> r),
                (e) =&gt; release(handleId).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">Promise</span>.reject(e)
          ))
        )
    }

    _fetchBlockeUsersData () {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-64">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-64">&#182;</a>
              </div>
              <p>get dataHandle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeDataId.getStructuredDataHandle(
            <span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._getLocation() + <span class="hljs-string">'_blocked_users'</span>, <span class="hljs-number">500</span>),</pre></div></div>
            
        </li>
        
        
        <li id="section-65">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-65">&#182;</a>
              </div>
              <p>replace dataHandle With structuredDataHandle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (dataHandle) =&gt; <span class="hljs-built_in">window</span>.safeStructuredData.getHandle(<span class="hljs-keyword">this</span>._authToken, dataHandle),</pre></div></div>
            
        </li>
        
        
        <li id="section-66">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-66">&#182;</a>
              </div>
              <p>release dataHandle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (dataHandle) =&gt; <span class="hljs-built_in">window</span>.safeDataId.dropHandle(<span class="hljs-keyword">this</span>._authToken, dataHandle))
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.__parsedResponseBody__.handleId)
        .then(<span class="hljs-function"><span class="hljs-params">handleId</span> =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-67">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-67">&#182;</a>
              </div>
              <p>keep the structured data handle around for later reuse</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle = handleId</pre></div></div>
            
        </li>
        
        
        <li id="section-68">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-68">&#182;</a>
              </div>
              <p>and read the data with it</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeStructuredData.readData(
              <span class="hljs-keyword">this</span>._authToken,
              <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle)
        })
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-69">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-69">&#182;</a>
              </div>
              <p>serialise the key then call fn</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _withSignedKey (signKeyHandle, fn) {
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeSignKey.serialise(<span class="hljs-keyword">this</span>._authToken, signKeyHandle)
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> (<span class="hljs-keyword">new</span> Buffer(res).toString(<span class="hljs-string">'base64'</span>)))
        .then(fn)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-70">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-70">&#182;</a>
              </div>
              <p>given a specific address, get a handle, read it, release the handle
and return the read Data</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _readAndRelease (address) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-71">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-71">&#182;</a>
              </div>
              <p>get dataHandle for id</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-built_in">window</span>.safeStructuredData.getHandle(<span class="hljs-keyword">this</span>._authToken, address),</pre></div></div>
            
        </li>
        
        
        <li id="section-72">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-72">&#182;</a>
              </div>
              <p>read structured data from dataHandle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        (handleId) =&gt; <span class="hljs-built_in">window</span>.safeStructuredData.readData(<span class="hljs-keyword">this</span>._authToken, handleId),</pre></div></div>
            
        </li>
        
        
        <li id="section-73">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-73">&#182;</a>
              </div>
              <p>release datahandle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        (hId) =&gt; <span class="hljs-built_in">window</span>.safeStructuredData.dropHandle(<span class="hljs-keyword">this</span>._authToken, hId)
      )
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-74">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-74">&#182;</a>
              </div>
              <p>fetch the comment at `index</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _fetchComment (index) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._autoRelease(</pre></div></div>
            
        </li>
        
        
        <li id="section-75">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-75">&#182;</a>
              </div>
              <p>get data handle for position</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">window</span>.safeAppendableData.getDataIdAt(
                <span class="hljs-keyword">this</span>._authToken, <span class="hljs-keyword">this</span>._currentPostHandleId, index),</pre></div></div>
            
        </li>
        
        
        <li id="section-76">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-76">&#182;</a>
              </div>
              <p>read data at position</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (dataid) =&gt; <span class="hljs-keyword">this</span>._readAndRelease(dataid),</pre></div></div>
            
        </li>
        
        
        <li id="section-77">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-77">&#182;</a>
              </div>
              <p>release data handle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          (dataIdHandle) =&gt; <span class="hljs-built_in">window</span>.safeDataId.dropHandle(<span class="hljs-keyword">this</span>._authToken, dataIdHandle))</pre></div></div>
            
        </li>
        
        
        <li id="section-78">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-78">&#182;</a>
              </div>
              <p>convert the given data to JSON</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-keyword">new</span> Buffer(data).toString()))
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-79">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-79">&#182;</a>
              </div>
              <p>we like our comments sorted by time</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _sortComments () {
      <span class="hljs-keyword">this</span>._data.commentList.sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>((b.data || b.comment).time) - <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>((a.data || a.comment).time)
      })
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-80">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-80">&#182;</a>
              </div>
              <p>refresh the blocked users structure</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _getBlockedUsersStructuredData () {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._fetchBlockeUsersData()
        .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> {
          <span class="hljs-keyword">this</span>._data.blockedUsers = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-keyword">new</span> Buffer(data).toString()) 
        })
        .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'comments-updated'</span>))
        .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> {
          <span class="hljs-built_in">console</span>.error(err);
        });
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-81">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-81">&#182;</a>
              </div>
              <p>let’s block a user</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _saveBlockedUser (userName, signKeyHandle) {
      <span class="hljs-keyword">this</span>._getCypher()
        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-82">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-82">&#182;</a>
              </div>
              <p>we already have a list of blocked users
update the block</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._blockedUserStructureDataHandle !== <span class="hljs-literal">null</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._withSignedKey(signKeyHandle, (serialisedSignKey) =&gt; {
              <span class="hljs-keyword">this</span>._data.blockedUsers[userName] = serialisedSignKey
              <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeStructuredData.updateData(
                  <span class="hljs-keyword">this</span>._authToken,
                  <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle,
                  <span class="hljs-keyword">new</span> Buffer(<span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-keyword">this</span>._data.blockedUsers)).toString(<span class="hljs-string">'base64'</span>), <span class="hljs-keyword">this</span>._symmetricCipherOptsHandle)
                .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> <span class="hljs-built_in">window</span>.safeStructuredData.post(
                    <span class="hljs-keyword">this</span>._authToken,
                    <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle)
                )
            })
          } <span class="hljs-keyword">else</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-83">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-83">&#182;</a>
              </div>
              <p>This is the first time a user is blocked, created
the block structure to keep track of the users blocked</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._withSignedKey(signKeyHandle, (serialisedSignKey) =&gt; {
              <span class="hljs-keyword">this</span>._data.blockedUsers = {}
              <span class="hljs-keyword">this</span>._data.blockedUsers[userName] = serialisedSignKey
              <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeStructuredData.create(
                  <span class="hljs-keyword">this</span>._authToken,
                  <span class="hljs-keyword">this</span>._getLocation() + <span class="hljs-string">'_blocked_users'</span>, <span class="hljs-number">500</span>,
                  <span class="hljs-keyword">new</span> Buffer(<span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-keyword">this</span>._data.blockedUsers)).toString(<span class="hljs-string">'base64'</span>),
                  <span class="hljs-keyword">this</span>._symmetricCipherOptsHandle)
                .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> { <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle = res.__parsedResponseBody__.handleId })
                .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> <span class="hljs-built_in">window</span>.safeStructuredData.put(
                      <span class="hljs-keyword">this</span>._authToken,
                      <span class="hljs-keyword">this</span>._blockedUserStructureDataHandle)
                )
            }
            )
          }
        });
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-84">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-84">&#182;</a>
              </div>
              <p>fetch the public names of the user</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _getDns () {
      MODULE.log(<span class="hljs-string">'Fetching DNS records'</span>)
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeDNS.getDns(<span class="hljs-keyword">this</span>._authToken)</pre></div></div>
            
        </li>
        
        
        <li id="section-85">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-85">&#182;</a>
              </div>
              <p>convert</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.__parsedResponseBody__)
        .then(<span class="hljs-function">(<span class="hljs-params">dnsData</span>) =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-86">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-86">&#182;</a>
              </div>
              <p>store dnsData on the user for later reuse</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">this</span>._data.user.dns = dnsData
          <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.isAdmin()) {</pre></div></div>
            
        </li>
        
        
        <li id="section-87">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-87">&#182;</a>
              </div>
              <p>As the admin, we should also read the blocked user structure
but do that once we are done with this cycle</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            <span class="hljs-built_in">window</span>.setTimeout(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>._getBlockedUsersStructuredData(), <span class="hljs-number">10</span>)
          }</pre></div></div>
            
        </li>
        
        
        <li id="section-88">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-88">&#182;</a>
              </div>
              <p>and emit an event so the UI can update</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'user-updated'</span>)
        })
        .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> {
          <span class="hljs-keyword">if</span> (err.message.indexOf(<span class="hljs-string">'401 Unauthorized'</span>) !== <span class="hljs-number">-1</span>) {
            <span class="hljs-keyword">return</span> (<span class="hljs-keyword">this</span>._authToken ? <span class="hljs-keyword">this</span>._authoriseApp() : <span class="hljs-keyword">this</span>.fetchComments());
          }
        });
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-89">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-89">&#182;</a>
              </div>
              <p>starting up, we need to authorise the app</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    _authoriseApp () {
      MODULE.log(<span class="hljs-string">'Authorising application'</span>)
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">window</span>.safeAuth.authorise(<span class="hljs-keyword">this</span>._data.appInfo, <span class="hljs-keyword">this</span>._LOCAL_STORAGE_TOKEN_KEY)</pre></div></div>
            
        </li>
        
        
        <li id="section-90">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-90">&#182;</a>
              </div>
              <p>convert tokeb</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> {
          <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> res === <span class="hljs-string">'object'</span>) {
            <span class="hljs-keyword">return</span> res.__parsedResponseBody__.token
          }
          <span class="hljs-keyword">return</span> res;
        })
        .then(<span class="hljs-function">(<span class="hljs-params">token</span>) =&gt;</span> {
          <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> token !== <span class="hljs-string">'string'</span>) {
            <span class="hljs-keyword">return</span>;
          }</pre></div></div>
            
        </li>
        
        
        <li id="section-91">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-91">&#182;</a>
              </div>
              <p>keep token for later reus`e</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">this</span>._authToken = token
          <span class="hljs-built_in">window</span>.safeAuth.setAuthToken(<span class="hljs-keyword">this</span>._LOCAL_STORAGE_TOKEN_KEY, token)
        })</pre></div></div>
            
        </li>
        
        
        <li id="section-92">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-92">&#182;</a>
              </div>
              <p>then refresh the DNS</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        .then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-keyword">this</span>._getDns())
        .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {</pre></div></div>
            
        </li>
        
        
        <li id="section-93">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-93">&#182;</a>
              </div>
              <p>something went terribly wrong,
remove the auth token</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-built_in">console</span>.error(err)
          <span class="hljs-keyword">this</span>._authToken = <span class="hljs-literal">null</span>
          <span class="hljs-keyword">this</span>.fetchComments();
          <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(err)
        })
    }
  }</pre></div></div>
            
        </li>
        
        
        <li id="section-94">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-94">&#182;</a>
              </div>
              <p>Expose the Controller to the global MODULE</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  MODULE.Controller = Controller
})(<span class="hljs-built_in">window</span>.safeComments)</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
